package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import play.db.DB;

import models.*;
import controllers.Application;
import utils.exceptions.BDException;


public class DisciplinaDAO {
	Connection conexao;
	
	public DisciplinaDAO() {
		conexao = DB.getConnection();
	}

	public boolean cadastrarDisciplina(Disciplina _disciplina) throws BDException {
		boolean retorno = false;
		
		String comando = "insert into disciplina (codDisc, nome, numAulas, dataInicio, dataFim)"+
		"values(?,?,?,?,?)";
	
		try {
			PreparedStatement p = this.conexao.prepareStatement(comando);
			
			p.setString(1, _disciplina.getCodigo());
			p.setString(2, _disciplina.getNome());
			p.setInt(3, _disciplina.getNumAulas());
			p.setDate(4, _disciplina.getDataInicio());
			p.setDate(5, _disciplina.getDataFim());
	
			p.execute();
			p.close();
			retorno = true;
		} catch (SQLException e) {
			
			throw new BDException("Ocorreu erro na hora de INSERIR a Disciplina (try catch)",e.getStackTrace());
		}
		
		return retorno;
	}
	
	public boolean editarDisciplina(Disciplina _disciplina) throws BDException{
		//boolean retorno = false;

		String comando = "update disciplina set nome=?, numAulas=?, dataInicio=?, dataFim=? where codDisc=?;";

		try {
			PreparedStatement p = this.conexao.prepareStatement(comando);

			p.setString(1, _disciplina.getNome());
			p.setInt(2, _disciplina.getNumAulas());
			p.setDate(3, _disciplina.getDataInicio());
			p.setDate(4, _disciplina.getDataFim());
			p.setString(5, _disciplina.getCodigo());
			p.execute();

			return true;
		} catch (SQLException e) {
			
			throw new BDException("Ocorreu erro na hora de EDITAR a Disciplina (try catch)",e.getStackTrace());
		}

	}

	public List<Disciplina> listarTodos() throws BDException {
		List<Disciplina> retorno = new ArrayList<Disciplina>();
		
		String comando = "select * from disciplina";
		
		try {
			java.sql.Statement start = conexao.createStatement();
			ResultSet rs = start.executeQuery(comando);
			
			while(rs.next()){
				Disciplina disciplina = new Disciplina();
				disciplina.setIdDisc(rs.getInt("idDisc"));
				disciplina.setCodigo(rs.getString("codDisc"));
				disciplina.setNome(rs.getString("nome"));
				disciplina.setNumAulas(rs.getInt("numAulas"));
				disciplina.setDataInicio(rs.getDate("dataInicio"));
				disciplina.setDataFim(rs.getDate("dataFim"));
			
				retorno.add(disciplina);
			}
		} catch (SQLException e) {
			
			throw new BDException("Ocorreu erro na hora de LISTAR a Disciplina (try catch)",e.getStackTrace());
		}
		
		return retorno;
	}
	
	public boolean excluirDisciplina(String _codigoDisciplina) throws BDException {
		boolean retorno = false;

		String comando = "delete from disciplina where codDisc=?;";
		try {
			PreparedStatement p = this.conexao.prepareStatement(comando);
			p.setString(1, _codigoDisciplina);

			p.execute();

			retorno = true;
		} catch (SQLException e) {
			
			throw new BDException("Ocorreu erro na hora de EXCLUIR a Disciplina (try catch)",e.getStackTrace());
		}

		return retorno;
	}

	public Disciplina buscarDisciplina(String codigoDisciplina) throws BDException {
		String comando = "select * from disciplina where codDisc='"+codigoDisciplina+"'";
		
		java.sql.Statement start;
		
		try {
			start = conexao.createStatement();
			ResultSet rs = start.executeQuery(comando);
			
			while(rs.next()){
				Disciplina disciplina = new Disciplina();
				disciplina.setIdDisc(rs.getInt("idDisc"));	
				disciplina.setCodigo(rs.getString("codDisc"));
				disciplina.setNome(rs.getString("nome"));
				disciplina.setNumAulas(rs.getInt("numAulas"));
				disciplina.setDataInicio(rs.getDate("dataInicio"));
				disciplina.setDataFim(rs.getDate("dataFim"));
			
				return disciplina;
			}
			
		} catch (SQLException e) {
			throw new BDException("Ocorreu erro na hora de BUSCAR a Disciplina (try catch)",e.getStackTrace());
		}
		return null;
	}


}